package com.gouda.core.dao;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.gouda.core.entity.MerchantEntity;
import org.amethyst.atlas.db.MagicDao;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by KGL on 2015/12/5.
 */
@Repository
public class MerchantDao extends MagicDao {

    private static final String QUERY_SQL = "select * from merchant t ";

    private static final String QUERY_COUNT = "select count(1) from merchant t ";

    public int seekCountByConditions(final String incCode, final String incName, final String state){
        StringBuilder whereSql = new StringBuilder("where 1=1 ");
        final Map<String, Object> params = Maps.newHashMap();
        if (Strings.isNullOrEmpty(incCode)){
            whereSql.append(" and t.inc_code = :incCode ");
            params.put("incCode", incCode);
        }
        if (Strings.isNullOrEmpty(incName)){
            whereSql.append(" and t.inc_name = :incName ");
            params.put("incName", incName);
        }
        if (Strings.isNullOrEmpty(state)){
            whereSql.append(" and t.state = :state ");
            params.put("state", state);
        }
        return super.count(MerchantEntity.class, QUERY_COUNT + whereSql.toString(), params);
    }

    public List<MerchantEntity> seekMerchantsByConditions(final String incCode, final String incName, final String state, final int start, final int size){
        StringBuilder whereSql = new StringBuilder("where 1=1 ");
        final Map<String, Object> params = Maps.newHashMap();
        if (Strings.isNullOrEmpty(incCode)){
            whereSql.append(" and t.inc_code = :incCode ");
            params.put("incCode", incCode);
        }
        if (Strings.isNullOrEmpty(incName)){
            whereSql.append(" and t.inc_name = :incName ");
            params.put("incName", incName);
        }
        if (Strings.isNullOrEmpty(state)){
            whereSql.append(" and t.state = :state ");
            params.put("state", state);
        }
        whereSql.append(" order by t.create_time desc limit :start,:size");
        params.put("start", start);
        params.put("size", size);
        return super.list(MerchantEntity.class, QUERY_SQL + whereSql.toString(), params);
    }

}
